package com.rongshu.view.discovery.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.ControlScrollLayoutManager;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.discovery.adapter.SerialChapterListAdapter;
import com.rongshu.view.discovery.bean.SerialChapterListResponse;
import com.rongshu.view.discovery.bean.SerialListResponse;
import com.rongshu.view.discovery.presenter.SerialDetailsPresenter;
import com.rongshu.view.home.activity.PreviewImgActivity;
import com.rongshu.view.home.widget.ExpandableTextView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2019/3/2.
 * description：连载列表
 */
public class SerialDetailsActivity extends BaseActivity<SerialDetailsPresenter> implements INetView {
    @BindView(R.id.head_left)
    RelativeLayout headLeft;
    @BindView(R.id.book_cover)
    ImageView bookCover;
    @BindView(R.id.book_name)
    TextView bookName;
    @BindView(R.id.book_author)
    TextView bookAuthor;
    @BindView(R.id.book_item)
    TextView bookItem;
    @BindView(R.id.book_description)
    ExpandableTextView bookDescription;
    @BindView(R.id.chapter_num)
    TextView bookItemNum;
    @BindView(R.id.chapter_list)
    RecyclerView chapterList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshLayout;
    private SerialChapterListAdapter adapter;
    private List<SerialChapterListResponse.ResponseBean> list;
    private SerialListResponse.ResponseBean bean;
    private int pageIndex = 1;
    private int pageSize = 10;

    @Override
    protected int initLayout() {
        return R.layout.activity_serial_details;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        bean = (SerialListResponse.ResponseBean) intent.getSerializableExtra(Constant.INTENT_DATA);
        //封面
        GlideUtils.loadImageView(this, bookCover, bean.getImgUrl());
        //书名
        bookName.setText(bean.getTitle());
        //作者
        bookAuthor.setText(bean.getAuthor());
        //简介
        bookDescription.setText(bean.getExplain());
        //章节
        int chapterNumber = bean.getChapterNumber();
        String chatperMsg = bean.getState() == 2
                ? "共" + chapterNumber + "章" : "更新至第" + chapterNumber + "章";
        bookItem.setText(chatperMsg);
        bookItemNum.setText("(共" + chapterNumber + "章)");
        //章节list
        list = new ArrayList<>();
        adapter = new SerialChapterListAdapter(this, list);
        ControlScrollLayoutManager manager = new ControlScrollLayoutManager(this);
        manager.setScrollEnabled(false);
        chapterList.setLayoutManager(manager);
        chapterList.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        headLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setPresenter(new SerialDetailsPresenter(this), this);
        if (bean != null) {
            presenter.getSerialChapterList(bean.getSerialize_id(), pageIndex, pageSize);
        }
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableAutoLoadmore(false);
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getSerialChapterList(bean.getSerialize_id(), pageIndex, pageSize);
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    public void success(Object object, int tag) {
        SerialChapterListResponse response = (SerialChapterListResponse) object;
        if (response == null) {
            return;
        }
        if (pageIndex == 1) {
            list.clear();
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadmore();
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
        List<SerialChapterListResponse.ResponseBean> itemBeanList = response.getResponse();
        list.addAll(itemBeanList);
        adapter.notifyDataSetChanged();
        if (itemBeanList.size() == 0) {
            refreshLayout.finishLoadmoreWithNoMoreData();
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }

    @OnClick(R.id.book_cover)
    public void onViewClicked() {
        ArrayList<String> listA = new ArrayList<>();
        listA.add(bean.getImgUrl());
        Intent intent = new Intent(this, PreviewImgActivity.class);
        intent.putStringArrayListExtra(Constant.INTENT_DATA, listA);
        startActivity(intent);
    }
}
